{{>header}}
import '{{dioImport}}';{{#useBuiltValue}}
import 'package:built_value/serializer.dart';
import 'package:{{pubName}}/src/serializers.dart';{{/useBuiltValue}}
import 'package:{{pubName}}/src/auth/api_key_auth.dart';
import 'package:{{pubName}}/src/auth/basic_auth.dart';
import 'package:{{pubName}}/src/auth/bearer_auth.dart';
import 'package:{{pubName}}/src/auth/oauth.dart';
{{#apiInfo}}{{#apis}}import 'package:{{pubName}}/src/api/{{classFilename}}.dart';
{{/apis}}{{/apiInfo}}
class {{clientName}} {
  static const String basePath = r'{{{basePath}}}';

  final Dio dio;
{{#useBuiltValue}}
  final Serializers serializers;

{{/useBuiltValue}}
  {{clientName}}({
    Dio? dio,{{#useBuiltValue}}
    Serializers? serializers,{{/useBuiltValue}}
    String? basePathOverride,
    List<Interceptor>? interceptors,
  })  : {{#useBuiltValue}}this.serializers = serializers ?? standardSerializers,{{/useBuiltValue}}
        this.dio = dio ??
            Dio(BaseOptions(
              baseUrl: basePathOverride ?? basePath,
              connectTimeout: 5000,
              receiveTimeout: 3000,
            )) {
    if (interceptors == null) {
      this.dio.interceptors.addAll([
        OAuthInterceptor(),
        BasicAuthInterceptor(),
        BearerAuthInterceptor(),
        ApiKeyAuthInterceptor(),
      ]);
    } else {
      this.dio.interceptors.addAll(interceptors);
    }
  }

  void setOAuthToken(String name, String token) {
    if (this.dio.interceptors.any((i) => i is OAuthInterceptor)) {
      (this.dio.interceptors.firstWhere((i) => i is OAuthInterceptor) as OAuthInterceptor).tokens[name] = token;
    }
  }

  void setBearerAuth(String name, String token) {
    if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
      (this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens[name] = token;
    }
  }

  void setBasicAuth(String name, String username, String password) {
    if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
      (this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password);
    }
  }

  void setApiKey(String name, String apiKey) {
    if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
      (this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys[name] = apiKey;
    }
  }{{#apiInfo}}{{#apis}}

  /// Get {{classname}} instance, base route and serializer can be overridden by a given but be careful,
  /// by doing that all interceptors will not be executed
  {{classname}} get{{classname}}() {
    return {{classname}}(dio{{#useBuiltValue}}, serializers{{/useBuiltValue}});
  }{{/apis}}{{/apiInfo}}
}
